package com.qiku.controller;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.qiku.entity.User;
import com.qiku.entity.UserDTO;
import com.qiku.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/authority")
@CrossOrigin  //解决跨域问题
public class AuthorityController {


    @Value("${ali.sms.signName}")
    private String signName;
    @Value("${ali.sms.templateCode}")
    private String templateCode;
    @Value("${ali.sms.assessKeyId}")
    private String assessKeyId;
    @Value("${ali.sms.assessKeySecret}")
    private String assessKeySecret;


    @Autowired
    private UserService userService;

    /**
     * 登录功能
     *
     * @param phone
     * @param password
     * @return
     */
    @GetMapping("/login")
    public UserDTO<User> login(String phone, String password) {
        return userService.login(phone, password);
    }

    @GetMapping("/checkToken")
    public UserDTO<User> checkToken(String token) {
        return userService.checkToken(token);
    }


    @GetMapping("sendSms")
    public Map<String, String> sendSms(String phoneNumber) {
        Map<String, String> map = new HashMap<>();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", assessKeyId, assessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int) (Math.random() * 10);
        }
        System.out.println("验证码：" + vcode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + vcode + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);

            System.out.println(response.getData());
            String jsonStr = response.getData();
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            if ("OK".equals(jsonObject.get("Message"))) {
                map.put("phoneNumber", phoneNumber);
                map.put("smsCode", vcode);
                map.put("Code", "OK");
                return map;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        map.put("message", "error");
        return map;
    }


    /**
     * 使用短信验证登录
     *
     * @param phoneNumber
     * @return
     */
    @GetMapping("/loginPhoneSms")
    public UserDTO<User> loginPhoneSms(String phoneNumber) {
        return userService.loginPhoneSms(phoneNumber);
    }
}

